//
//  BanBu_RegisterCell.m
// Halfarea
//
//  Created by banbu3 on 14-2-10.
//
//

#import "BanBu_RegisterCell.h"

@implementation BanBu_RegisterCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        self.backgroundColor = [UIColor clearColor];
        UIView *b = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
        b.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:b];
        self.backView = b;
        [b release];
        
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 0,290, 70)];
        self.textField = textField;
        textField.font = [UIFont boldSystemFontOfSize:19];
        textField.delegate = self;
        textField.backgroundColor = [UIColor clearColor];
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.borderStyle = UITextBorderStyleNone;
        textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        textField.textColor = [UIColor blackColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        //    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        //    textField.returnKeyType = UIReturnKeyNext;
        [self.contentView addSubview:textField];
        [textField addTarget:self action:@selector(textFieldValueDidChange:) forControlEvents:UIControlEventEditingChanged];
        [textField release];
        
        UILabel *errorLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 230, 70)];
        errorLab.backgroundColor = [UIColor clearColor];
        errorLab.font = [UIFont boldSystemFontOfSize:19];
        errorLab.textColor = [UIColor whiteColor];
        [self.contentView addSubview:errorLab];
        errorLab.hidden = YES;
        self.errorLabel = errorLab;
        
        
        
    }
    return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setError
{
    self.textField.hidden = self.textField.text.length?YES:NO;
    self.errorLabel.hidden = NO;
    self.errorLabel.text = self.textField.text;
    self.backView.backgroundColor = [UIColor redColor];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelError:)];
    self.errorLabel.userInteractionEnabled = YES;
    [self.errorLabel addGestureRecognizer:tap];
    [tap release];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(260, 22.5, 25, 25);
    [btn setBackgroundImage:[UIImage imageNamed:@"cancelAll.png"] forState:UIControlStateNormal];
    [self.backView addSubview:btn];
    [btn addTarget:self action:@selector(cancelAll) forControlEvents:UIControlEventTouchUpInside];
    cancelButton = btn;
}
-(void)cancelAll
{
    self.textField.text = @"";
    [cancelButton removeFromSuperview];
    [self cancelError:nil];
}
-(void)cancelError:(UIGestureRecognizer *)_ges
{
    if(_ges)
    {
        [self.errorLabel removeGestureRecognizer:_ges];
    }
    [self.textField becomeFirstResponder];
    self.textField.hidden = NO;
    self.errorLabel.hidden = YES;
    self.errorLabel.text = self.textField.text;
    self.backView.backgroundColor = [UIColor whiteColor];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.delegate cellTextFieldBeginEditing:textField andCell:self];
}

-(void)textFieldValueDidChange:(UITextField *)textField
{
    [self.delegate cellTextFieldDidEditing:self.textField];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if([self.delegate respondsToSelector:@selector(cellTextField:shouldChangeCharactersInRange:replacementString:)])
    {
                return [self.delegate cellTextField:textField shouldChangeCharactersInRange:range replacementString:string];
    }
    
//        return [self.delegate cellTextField:textField shouldChangeCharactersInRange:range replacementString:string];
    
    return YES;
    
}
@end
